package com.glsc.ngateway.opmanage.service.report.excel.listener.regulatory;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.glsc.ngateway.common.base.domain.mysql.opmanage.report.fileupload.ImportTemplateRulesDefine;
import com.glsc.ngateway.common.base.domain.mysql.opmanage.report.regulatory.RegRptBondMng;
import com.glsc.ngateway.common.base.repo.mysql.opmanage.report.regulatory.RegRptBondMngRepo;
import com.glsc.ngateway.common.base.utils.ExcelDoubleConverter;
import com.glsc.ngateway.opmanage.service.report.excel.listener.TemplateCheckHandler;
import org.springframework.util.CollectionUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: qiyr
 * @date: 2022/8/31 14:38
 * @description: 债券业务管理系统数据导入监听器
 */
public class RegRptBondMngListener extends AnalysisEventListener<Map<Integer, String>> {

    private static final int BATCH_COUNT = 3000;
    /**
     * 报表Excel文件数据区域rowIndex
     */
    private static final List<Integer> dataRowIdList =
            Arrays.asList(3, 4, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 20, 25, 26, 27, 28, 32, 33, 34);
    List<RegRptBondMng> list = new ArrayList<>();

    private List<ImportTemplateRulesDefine> rulesDefineList;
    private RegRptBondMngRepo regRptBondMngRepo;
    private BigInteger mainId;

    public RegRptBondMngListener() {
    }

    public RegRptBondMngListener(RegRptBondMngRepo regRptBondMngRepo, BigInteger mainId,
                                 List<ImportTemplateRulesDefine> rulesDefineList) {
        this.regRptBondMngRepo = regRptBondMngRepo;
        this.mainId = mainId;
        this.rulesDefineList = rulesDefineList;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        TemplateCheckHandler.check(context, rulesDefineList);
        ReadRowHolder readRowHolder = context.readRowHolder();
        Integer rowIndex = readRowHolder.getRowIndex();
        Map<Integer, Cell> dataMap = readRowHolder.getCellMap();

        if (dataRowIdList.contains(rowIndex)) {
            list.add(new RegRptBondMng(mainId, rowIndex + 1, data.get(0),
                    ExcelDoubleConverter.cellToDouble(dataMap, 1),
                    ExcelDoubleConverter.cellToDouble(dataMap, 2),
                    ExcelDoubleConverter.cellToDouble(dataMap, 3)));
        }

        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("导入失败，数据量为空，请检查文件格式与内容");
        }
        regRptBondMngRepo.saveAll(list);
    }
}
